<template>
    <div class="order-report">
        <div class="report-table-top-title">
            历史记录
        </div>
        <div class="report-table-form1">
            <div class="report-table-form1-line">
                <div class="report-table-form1-item word samll" style="width: 70px">选择商家:</div>
                <div class="report-table-form1-item" style="width: 180px">
                    <el-select v-model="companyCode" placeholder="请选择" size="small">
                        <el-option
                            v-for="item in companyList"
                            :key="item.companyCode"
                            :label="item.companyName"
                            :value="item.companyCode"
                        ></el-option>
                    </el-select>
                </div>
                <!--<div class="report-table-form1-item word samll" style="width: 80px">-->
                <!--商家编号：-->
                <!--</div>-->
                <!--<div class="report-table-form1-item" style="width: 180px">-->
                <!--<el-input v-model="searchName" placeholder="" size="medium" suffix-icon="el-icon-search"></el-input>-->
                <!--</div>-->
            </div>
            <div class="report-table-form1-line">
                <div class="report-table-form1-item word samll" style="width: 70px">变动日期:</div>
                <div class="report-table-form1-item" style="width: 200px">
                    <el-date-picker
                        size="small"
                        v-model="timeStart"
                        type="date"
                        value-format="yyyy-MM-dd 00:00:00"
                        placeholder="选择日期"
                    ></el-date-picker>
                </div>
                <div class="report-table-form1-item word samll" style="width: 20px">至</div>
                <div class="report-table-form1-item" style="width: 200px">
                    <el-date-picker
                        size="small"
                        v-model="timeEnd"
                        type="date"
                        value-format="yyyy-MM-dd 23:59:59"
                        placeholder="选择日期"
                    ></el-date-picker>
                </div>
                <div class="report-table-form1-item word samll" style="width: 40px"></div>
                <el-button type="primary" size="small" @click="search">搜索</el-button>
                <el-button type="primary" size="small" @click="toMain">返回</el-button>
            </div>
        </div>
        <div class="page-padding-gray-small"></div>
        <div class="report-table-wrapper">
            <el-table
                :header-cell-style="headerCellStyle"
                size="small"
                :data="showList"
                border
                style="width: 100%"
            >
                <el-table-column prop="addTime" label="变动时间" width="200">
                    <template slot-scope="props">
                        <div>{{$F(props.row.addTime)}}</div>
                    </template>
                </el-table-column>
                <el-table-column prop="content" label="变动内容">
                    <template slot-scope="props">
                        <div
                            style="width: 600px;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;"
                        >{{props.row.content}}</div>
                    </template>
                </el-table-column>
                <el-table-column type="expand">
                    <template slot-scope="props">
                        <div style="line-height: 18px;font-size: 16px">{{props.row.content}}</div>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="report-table-block">
            <div class="report-table-block-in">
                <el-pagination
                    background
                    :current-page.sync="position"
                    @current-change="handleCurrentChange"
                    :page-size="10"
                    layout="prev, pager, next, total"
                    :total="totalNum"
                ></el-pagination>
            </div>
        </div>

        <div class="page-padding"></div>
    </div>
</template>

<script type="text/ecmascript-6">
import fileDownload from 'js-file-download'
import axios from 'axios'
export default {
    data: function () {
        return {
            timeStart: '',
            timeEnd: '',

            companyCode: '',
            companyList: [],

            nowPage: 0, // 当前所在页面

            showList: [],

            totalNum: 0,
            position: 1, // 页码

            searchNo: '',
            searchName: ''
        }
    },
    mounted () {
    },
    created () {
        this.getAllCompany()
        this.companyCode = this.companyCodeOut
        this.setDefaultTime()
        this.getPage()
    },
    watch: {
        position () {
            this.getPage()
        }
    },
    components: {
    },
    props: {
        underLine: null,
        managementType: Number
    },
    computed: {
        companyCodeOut () {
            return this.$store.state.page1KTVAgentHistory
        }
    },
    methods: {
        toTimeS1 (date) {
            return date.getFullYear() + '-' + (this.outTen(date.getMonth() + 1)).toString() + '-' + (this.outTen(date.getDate())).toString() + ' 00:00:00'
        },
        toTimeS2 (date) {
            return date.getFullYear() + '-' + (this.outTen(date.getMonth() + 1)).toString() + '-' + (this.outTen(date.getDate())).toString() + ' 23:59:59'
        },
        outTen (item) {
            if (item < 10) {
                return '0' + item
            } else {
                return item
            }
        },
        getAllCompany () {
            this.$http.post(this.$store.state.IP + '/system/company/getAllCompanyByAgent', {
                data: {
                    managementType: this.managementType
                }
            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                console.log('-------------------------------------------')
                response = response.body
                console.log(response)
                if (response.ret === '0') {
                    console.log('调用获取所有代理商成功')
                    console.log(response.data)
                    this.companyList = response.data
                }
                if (response.ret !== '0') {
                    console.log('调用获取所有代理商失败')
                }
                console.log('******************************************')
            }, response => {
                console.log('通讯失败')
            })
        },

        toMain () {
            this.$store.dispatch('setKA', 0)
        },

        setDefaultTime () {
            let time = new Date()
            time.setDate(time.getDate() - 1)
            this.timeStart = this.toTimeS1(time)
            let time2 = new Date()
            time2.setDate(time.getDate() + 1)
            this.timeEnd = this.toTimeS2(time2)
        },

        search () {
            this.position = 1
            if (this.timeStart > this.timeEnd) {
                this.$message({
                    message: '开始时间不能大于结束时间',
                    type: 'warning'
                })
                return
            }
            this.getPage()
        },
        exportRport () {
            let that = this
            axios.get(this.$store.state.IP + '/report/export/roomReserved' +
                '&searchName=' + this.searchName +
                '&startTime=' + this.dateToString(this.timeStart) +
                '&endTime=' + this.dateToString(this.timeEnd),
                {
                    responseType: 'blob',
                    headers: that.axiosDownloadHeaders()
                }
            ).then(function (response) {
                console.log(response)
                if (response.headers['content-type'] === 'application/json;charset=UTF-8') {
                    console.log('出错')
                    return
                }
                let fileName = decodeURIComponent(response.headers['content-disposition'])
                fileName = fileName.substring(20)
                if (response.headers['content-type'] === 'application/x-msdownload;charset=UTF-8') {
                    fileDownload(response.data, fileName)
                }
            }).catch(function (error) {
                alert(error)
            })
        },

        getPage () {
            this.$http.post(this.$store.state.IP + '/system/company/modifyHistoryPage', {
                data: {
                    page: this.position - 1, // 页数
                    limit: 10, // 一页显示数量
                    companyCode: this.companyCode,
                    endTime: this.timeEnd,
                    startTime: this.timeStart
                }
            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                console.log('-------------------------------------------')
                response = response.body
                console.log(response)
                if (response.ret === '0') {
                    console.log('调用出品详情成功')
                    console.log(response.data)
                    this.showList = response.data.list
                    this.totalNum = response.data.total
                }
                if (response.ret !== '0') {
                    console.log('调用出品详情失败')
                }
                console.log('******************************************')
            }, response => {
                console.log('通讯失败')
            })
        },

        // 换页方法
        handleCurrentChange (val) {
            this.position = val
        }
    }
}
</script>

<style lang="stylus" rel="stylesheet/stylus">
.order-report
    width: 100%
    background #f1f0f5

    .report-table-top-title
        width: 100%
        height: 64px
        line-height: 64px
        color: #000
        font-size: 18px

     .report-table-form1
        width: 100%
        min-height: 40px
        padding: 16px 0 0 22px
        box-sizing border-box
        background #fff
        overflow hidden

        .report-table-form1-line
            min-height: 32px
            margin-bottom: 16px

            .report-table-form1-item
                display: inline-block
                vertical-align: top
                height: 32px
                margin-right: 15px
                line-height: 32px

                &.word
                    margin-right: 0

                &.samll
                    font-size: 15px
                    text-align left

                &.big
                    margin-right: 100px

    .report-table-block
        width: 100%
        padding-top: 22px
        height 50px
        background #fff
        box-sizing border-box

        .report-table-block-in
            float: right
            padding-right 17px

    .page-padding-gray-small
        width: 100%
        height: 22px
        background #f1f0f5
    .page-padding
        width: 100%
        height: 20px
        background #fff


    .report-table-total
        width: 100%
        font-size: 16px
        color #409EFF
        padding: 22px
        margin-top 22px
        box-sizing border-box
        background #fff
</style>
